local desc_huan1v1 = [[
  # 星汉灿烂·忠武模式简介

  两人进行对战的竞技化模式，进行车轮战，最多进行5个小局，获胜的武将组合称为“跑掉”，且本局游戏不能再选用；先跑掉5个其他武将+于吉的玩家胜利。

  ___

  ## 游戏流程

  1. **决定行动顺序**。随机决定先手角色和后手角色。

  2. **挑选武将**。每局开始时先给双方各发10张武将牌，选择两张ban掉以后拿给对手作为将池，双方再各自获得一张“于吉”。

  3. **选择1~2名登场武将**。双方各从自己所拥有的10张武将牌中选择首发武将，同时正面朝上亮出。

  4. **分发起始手牌**。双方各自摸4张牌，作为其起始手牌，体力上限为武将体力上限相加除以2（向下取整），若有余数则起始手牌数+1。。

  5. **游戏开始**。由先手玩家先开始自己的回合，且首回合摸牌阶段摸牌数-1。后手玩家回合结束后，先手玩家回合开始，依次轮流直到本小局结束。

  6. **武将死亡**。当某一角色的武将死亡时，若游戏未结束，则弃置其区域内的所有牌，由该玩家选择接下来的登场武将，然后并摸起始手牌。特别地，如果玩家在自己的回合内武将死亡，则其回合立即结束。对手本小局获胜的武将于本局游戏中不能再次使用。

  7. **游戏结束**。当某一角色的获胜武将池有于吉和5个其他武将，则其获胜，游戏结束。

]]

local U = require "packages/utility/utility"

  local huan_1v1_logic = function()
    local huan_1v1_logic = GameLogic:subclass("huan_1v1_logic")

    --开局选将
  function huan_1v1_logic:chooseGenerals()
    local room = self.room ---@type Room

    --设置身份
    local lord = room.players[1]
    room:setCurrent(lord)
    local nonlord = room.players[2]

    room:setPlayerProperty(nonlord, "role_shown", true)
    room:broadcastProperty(nonlord, "role")

    --两人分别的武将池
    local all_lord_generals = room:getNGenerals(10)
    local all_nolord_generals = room:getNGenerals(10)
    --分别存储双方开局的将池
    local mark1 = U.getPrivateMark(lord, "&all_generals")
    local mark2 = U.getPrivateMark(nonlord, "&all_generals")
    table.insertTableIfNeed(mark1, all_lord_generals)
    U.setPrivateMark(lord, "&all_generals", mark1)
    table.insertTableIfNeed(mark2, all_nolord_generals)
    U.setPrivateMark(nonlord, "&all_generals", mark2)
    --同时ban两名武将
    local req = Request:new(room.players, "CustomDialog")
    for _, p in ipairs(room.players) do
      local data = {
        path = "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml",
        data = {
          U.getPrivateMark(p, "&all_generals"),
        {"OK"},
        "#huan1v1_ban_general-choosen",
        {},
        2,
        2,
        {}
      },
      }
      req:setData(p, data)
    end
    req.focus_text = "#huan1v1_ban_general"

    req:ask()

    for _, p in ipairs(room.players) do
      local result = req:getResult(p)
      local choice
      if result ~= "" then
        local reply = result
        choice = reply.cards
      else
        choice = table.random(U.getPrivateMark(p, "&all_generals"), 2)
      end
      local mark = U.getPrivateMark(p, "&all_generals")
      table.removeOne(mark, choice[1])
      table.removeOne(mark, choice[2])
      U.setPrivateMark(p, "&all_generals", mark)
    end

    --双方将池互换
    mark1 = U.getPrivateMark(lord, "&all_generals")
    mark2 = U.getPrivateMark(nonlord, "&all_generals")
    --插入于吉
    table.insert(mark1, "huan_1v1__yuji")
    table.insert(mark2, "huan_1v1__yuji")
    U.setPrivateMark(lord, "&all_generals", mark2)
    U.setPrivateMark(nonlord, "&all_generals", mark1)
    --公开双方将池
    U.showPrivateMark(lord, "&all_generals")
    U.showPrivateMark(nonlord, "&all_generals")

    --存储双方的将池（方便规则）
    local updataGeneralPile = function(p)
      if p == lord then
        room:setBanner("@&lord_all_generals", U.getPrivateMark(lord, "&all_generals"))
      else
        room:setBanner("@&nonlord_all_generals", U.getPrivateMark(nonlord, "&all_generals"))
      end
    end

    --选择武将（第一小局开始）
    room:sendLog{
      type = "#1v1_getyuji",
      toast = true,
    }
    room:doBroadcastNotify("ShowToast", Fk:translate("huan1v1_FirstRound"))
    room:setBanner("@huan1v1Counts", 1) --局数统计
    room:addPlayerMark(lord, "huan1v1Counts", 1) --方便局数更新
    room:addPlayerMark(nonlord, "huan1v1Counts", 1)
    --选择本小局上几个武将
    req = Request:new(room.players, "AskForChoice")
    local choices = {"huan1v1-One","huan1v1-Doubles"}
    req:setData(lord, {choices, choices, "#huan1v1-SelectGeneral", "#huan1v1-SelectNumber"})
    req:setData(nonlord, {choices, choices, "#huan1v1-SelectGeneral", "#huan1v1-SelectNumber"})
    req:setDefaultReply(lord, "huan1v1-One")
    req:setDefaultReply(nonlord, "huan1v1-One")

    req.focus_text = "#huan1v1-SelectNumber"
    req.receive_decode = false
    req:ask()

    for _, p in ipairs(room.players) do
      local result = req:getResult(p)
      if result == "huan1v1-One" then
         room:setPlayerMark(p, "huan1v1-GeneralsNumber", 1)
      else
         room:setPlayerMark(p, "huan1v1-GeneralsNumber", 2)
      end
    end

    req = Request:new(room.players, "AskForGeneral")
    mark1 = U.getPrivateMark(lord, "&all_generals")
    mark2 = U.getPrivateMark(nonlord, "&all_generals")
    local x = lord:getMark("huan1v1-GeneralsNumber")
    local y = nonlord:getMark("huan1v1-GeneralsNumber")
    req:setData(lord, { mark1, x })
    req:setData(nonlord, { mark2, y })
    req:setDefaultReply(lord, { mark1[1] })
    req:setDefaultReply(nonlord, { mark2[1] })
    req:ask()

    room:broadcastProperty(lord, "role")
    room:broadcastProperty(nonlord, "role")

    --分配主副将
    for _, p in ipairs(room.players) do
      local main = req:getResult(p)[1]
      room:setPlayerGeneral(p, main, true, true)
      room:broadcastProperty(p, "general")
      room:setPlayerMark(p, "1v1_general", main)
      if p:getMark("huan1v1-GeneralsNumber") > 1 then
      local deputy = req:getResult(p)[2]
      room:setDeputyGeneral(p, deputy)
      room:broadcastProperty(p, "deputyGeneral")
      room:setPlayerMark(p, "1v1_deputy", deputy)
      end
    end

    room:broadcastProperty(lord, "kingdom")
    room:broadcastProperty(nonlord, "kingdom")
    --先手标记
    room:setBanner("1v1firstRound-lord", 1)

    updataGeneralPile(lord)
    updataGeneralPile(nonlord)
    --后续更改座位号即可
    room:askToChooseKingdom(room.players)
  end

function huan_1v1_logic:broadcastGeneral()
  local room = self.room
  local players = room.players

   --重新计算体力值(每小局选完将后都需要重新计算)
   for _, p in ipairs(players) do
    if p:getMark("huan1v1-GeneralsNumber") == 2 then --双将
       assert(p.general ~= "")
       local general = Fk.generals[p:getMark("1v1_general")]
       local deputy = Fk.generals[p:getMark("1v1_deputy")]
       local dmaxHp = deputy.maxHp
       local gmaxHp = general.maxHp
       p.maxHp = (dmaxHp + gmaxHp) // 2
       p.hp = p.maxHp
       p.shield = 0
       room:broadcastProperty(p, "general")
       room:broadcastProperty(p, "deputyGeneral")
       room:broadcastProperty(p, "maxHp")
       room:broadcastProperty(p, "hp")
       room:broadcastProperty(p, "shield")
       if (dmaxHp + gmaxHp) % 2 == 1 then
          room:setPlayerMark(p, "yuji1v1-ExtraHP", 1) --体力值余数标记，用于起始手牌+1
       end
     else
       assert(p.general ~= "")
       local general = Fk.generals[p:getMark("1v1_general")]
       p.maxHp = general.maxHp
       p.hp = p.maxHp
       p.shield = 0
       room:broadcastProperty(p, "general")
       room:broadcastProperty(p, "maxHp")
       room:broadcastProperty(p, "hp")
       room:broadcastProperty(p, "shield")
     end
  end

end

  return huan_1v1_logic
end

local huan_1v1_mode = fk.CreateGameMode{
  name = "huan_1v1_mode",
  minPlayer = 2,
  maxPlayer = 2,
  logic = huan_1v1_logic,
  rule = "#huan_1v1_rule&",
  surrender_func = function(self, playedTime)
    return { { text = "time limitation: 2 min", passed = playedTime >= 120 } }
  end,
}

Fk:loadTranslationTable{
    ["huan_1v1_mode"] = "星汉灿烂·忠武",
    [":huan_1v1_mode"] = desc_huan1v1,

    ["@[private]&all_generals"] = "备选区",
    ["@&huan1V1-lordGenerals"] = "主公跑掉武将",
    ["@&huan1v1-nonlordGenerals"] = "反贼跑掉武将",
    ["@&lord_all_generals"] = "主公备选区",
    ["@&nonlord_all_generals"] = "反贼备选区",
    ["@huan1v1Counts"] = "当前局数",

    ["#huan1v1_ban_general"] = "禁用武将",
    ["#huan1v1_ban_general-choosen"] = "请给对手禁用两名武将",

    ["huan1v1_FirstRound"] = "<b><font color='red'>第一局开始！</font></b>",
    ["#1v1_getyuji"] = "双方将池互换，并额外获得一张 <b><font color=\"#DC143C\">于吉<b>",

    ["huan1v1-One"] = "出场单将",
    ["huan1v1-Doubles"] = "出场双将",
    ["#huan1v1-SelectNumber"] = "选择武将数",
    ["#huan1v1-SelectGeneral"] = "选择武将",
    ["huan1v1-First"] = "先手",
    ["huan1v1-Second"] = "后手",
    ["#huan1v1-select_seat"] = "选择先后手",
    ["#yuji1v1-first-select"] = "%from 选择了 <b><font color='red'>先手</font></b>",
    ["#yuji1v1-second-select"] = "%from 选择了 <b><font color='red'>后手</font></b>",
}

return huan_1v1_mode